package com.cyyz.service.Impl;

import com.cyyz.dao.TagDao;
import com.cyyz.entity.Tag;
import com.cyyz.service.TagService;
import com.cyyz.util.StringInterconversionList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TagServiceImpl
 * @Description TODO
 * @Author 简笔轩客
 * @Date 2020/5/31 10:15
 * @Version 1.0
 */

@Service
public class TagServiceImpl implements TagService {
    
    @Autowired
    private TagDao tagDao;

    @Transactional
    @Override
    public int saveTag(Tag Tag) {
        return tagDao.saveTag(Tag);
    }

    @Transactional
    @Override
    public Tag getTag(Long id) {
        return tagDao.getTag(id);
    }

    @Transactional
    @Override
    public List<Tag> getAllTag() {
        return tagDao.getAllTag();
    }

    @Override
    public List<Tag> getAllTagAndBlog() {
        return tagDao.getAllTagAndBlog();
    }

    @Override
    public List<Tag> getTagByBlogId(Long id) {
        return tagDao.getTagByBlogId(id);
    }

    @Transactional
    @Override
    public List<Tag> getTagByString(String ids) {
        List<Tag> tags = new ArrayList<>();;
        Map<Integer, List> map = StringInterconversionList.stringToList(ids);
        if (map.get(0).size()!=0)
            tags = tagDao.getTagByString(map.get(0));
        if (map.get(1).size()!=0){
            for (Object o : map.get(1)) {
                Tag tag = new Tag();
                tag.setName(o.toString());
                saveTag(tag);
                tags.add(tag);
            }
        }
        return tags;
    }

    @Override
    public Tag getTagByName(String name) {
        return tagDao.getTagByName(name);
    }

    @Transactional
    @Override
    public int updateTag(Tag Tag) {
        return tagDao.updateTag(Tag);
    }

    @Transactional
    @Override
    public void deleteTag(Long id) {
        tagDao.deleteTag(id);
    }

}
